package com.liu.controller;


import com.liu.common.domain.ResultVo;
import com.liu.config.BaiduConfig;
import com.liu.domain.FileUploadResult;
import com.liu.domain.Token;
import com.liu.service.BaiduService;
import com.liu.service.BaiduServiceCurlImpl;
import com.liu.common.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/baidu")
@Slf4j
public class BaiduController {

    @Autowired
    BaiduConfig baiduConfig;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    BaiduService baiduService;

    @Autowired
    BaiduServiceCurlImpl baiduServiceCurl;

    @Autowired
    RedisTemplate redisTemplate;

    public static final Map<String,String> map = new HashMap<>();



    @GetMapping("/keys")
    public Object getBaiduKeys(){

        System.out.println(baiduConfig.toString());
        return baiduConfig.toString();

    }
    @GetMapping("/getCodeUrl")
    public String testRestTemplate(){
       String url= baiduService.makeCodeUrl(null);
        System.out.println(url);


        return url;
    }



    @GetMapping("/code")
    public Token getAccess_token(String code){
//        code ="db8b4c3e0b01f8aa3c15ab38473e5d00";
        String url= baiduService.makeAccessCodeURl(code);
        Token  token =restTemplate.getForObject(url,Token.class);
//        Token  token = JacksonUtil.readValue(result,Token.class);
         log.info("【TOKEN】"+ JacksonUtil.printJson(token));

        redisTemplate.opsForValue().set("token",token);
        return   token;
    }


    @GetMapping("/userInfo")
    public Object getUserInfo(){


        return baiduService.getUserInfo();

    }

    @GetMapping("/usAge")
    public String usAge(){

        return baiduService.usAge();

    }


    @GetMapping("/getFileList")
    public String getfileList(String path)throws  Exception{
            return baiduService.getFileList(path);
    }


    @PostMapping("/uploadFileCurl")
    public String uploadFile(String path) throws  Exception{
           File file = new File("d:/1605066997698.png");
        return  baiduServiceCurl.uploadCurl(file);




    }

    @PostMapping("/uploadFileJava")
    public ResultVo uploadFilejava(String path) throws  Exception{
        File file = new File("d:/这个是中文测试.md");
        FileUploadResult result = baiduService.uploadFile("/apps/",file);
        return ResultVo.success(result);




    }






    @GetMapping("/refreshToken")
    public Token refreshToken() {

        return baiduService.getAccessTokeByrefresh();

    }








}
